#include "netim.h"

int netim::mode = 0;

netim::netim(SettingManager *settings, QWidget *parent)
    : QMainWindow(parent)
{
	ui.setupUi(this);
        QMenu *submenu = this->contextMenu.addMenu(QString(tr("Change mode")));
        this->contextMenu.addSeparator();
        this->contextMenu.addAction(ui.actionExit);
        QAction *vis = submenu->addAction(QIcon(":/images/mverde.png"), QString(tr("Visible")));
        QAction *ami = submenu->addAction(QIcon(":/images/mazul.png"), QString(tr("Only friends")));
        QAction *inv = submenu->addAction(QIcon(":/images/mrojo.png"), QString(tr("Invisible")));
        connect(vis, SIGNAL(triggered()), this, SLOT(ch_visible()));
        connect(ami, SIGNAL(triggered()), this, SLOT(ch_amigo()));
        connect(inv, SIGNAL(triggered()), this, SLOT(ch_invisible()));
        this->setAttribute(Qt::WA_DeleteOnClose, false);
        this->tcppubliccontroller = new TcpPublicController();
        this->settings = settings;
        this->contactlistmanager =
                new ContactListManager::ContactListManager(ui.contactList);
        this->public_scanner = new scanner::scanner(this->tcppubliccontroller->getListeningPort(),
                                                    5544, this->contactlistmanager,
                      ui.visibilityStatus->currentIndex(), this->settings->alias);
        this->scanner_updater.setSingleShot(false);
        connect(&scanner_updater, SIGNAL(timeout()), this, SLOT(update_public_scanner()));
        this->scanner_updater.start(netim::step_time);
        this->setWindowTitle(QString("Netim - ").append(settings->alias));
        this->trayIcon.setParent(this);
        this->trayIcon.setIcon(QIcon(":/images/mrojo.png"));
        this->trayIcon.setToolTip(QString("NetIM"));
        this->trayIcon.setContextMenu(&(this->contextMenu));
        this->trayIcon.setVisible(true);
        connect(&(this->trayIcon), SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
                this, SLOT(tray_activated(QSystemTrayIcon::ActivationReason)));
}

void netim::tray_activated(QSystemTrayIcon::ActivationReason reason)
{
    if ((reason == QSystemTrayIcon::Trigger) || (reason == QSystemTrayIcon::DoubleClick)) {
        if (this->isActiveWindow()) {
            this->hide();
        } else {
            this->show();
            this->activateWindow();
            this->raise();
            this->setWindowState((this->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
        }
        return;
    }
}

void netim::ch_visible()
{
    this->ui.visibilityStatus->setCurrentIndex(2);
}

void netim::ch_amigo()
{
    this->ui.visibilityStatus->setCurrentIndex(1);
}

void netim::ch_invisible()
{
    this->ui.visibilityStatus->setCurrentIndex(0);
}

void netim::update_public_scanner()
{
    this->public_scanner->update();
}

void netim::on_actionExit_activated()
{
        this->trayIcon.hide();
	this->close();
        QApplication::exit(0);
}

netim::~netim()
{
    this->trayIcon.hide();
    delete this->contactlistmanager;
    delete this->public_scanner;
    exit(0);
}

void netim::on_visibilityStatus_currentIndexChanged(int index)
{
    this->public_scanner->switchmode(index);
    netim::mode = index;
    switch (index) {
            case 0:
                trayIcon.setIcon(QIcon(":/images/mrojo.png"));
                break;
            case 1:
                trayIcon.setIcon(QIcon(":/images/mazul.png"));
                break;
            case 2:
                trayIcon.setIcon(QIcon(":/images/mverde.png"));
                break;
    }
}

void netim::on_contactList_doubleClicked(QModelIndex index)
{
    this->contactlistmanager->startConversation(index.row());
}

